# Maintainer: Ray Donnelly <mingw.android@gmail.com>

_realname=nlopt
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.4.2
pkgrel=3
pkgdesc="A library for nonlinear optimization (mingw-w64)"
arch=('any')
makedepends=("${MINGW_PACKAGE_PREFIX}-swig")
url='http://ab-initio.mit.edu/wiki/index.php/NLopt'
license=('LGPLv2.1+')

source=(#"https://github.com/stevengj/nlopt/archive/${_realname}-${pkgver}.tar.gz"
        "http://ab-initio.mit.edu/${_realname}/${_realname}-${pkgver}.tar.gz"
        "config.sub"
        "config.guess")

sha256sums=('8099633de9d71cbc06cd435da993eb424bbcdbded8f803cdaa9fb8c6e09c8e89'
            '75d5d255a2a273b6e651f82eecfabf6cbcd8eaeae70e86b417384c8f4a58d8d3'
            '467e0886e3d4b06087e704dc9e22bbb8f93eb8c7b81aeae1ee0000d06aa4c45f')

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  #cp -f "${srcdir}"/config.{sub,guess} .
  #./autogen.sh --no-configure
}

build() {
  cd "$srcdir"/${_realname}-${pkgver}
  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf "${srcdir}"/build-${CARCH}
  mkdir -p "${srcdir}"/build-${CARCH} && cd "${srcdir}"/build-${CARCH}

  # If switching back to the github 'release' tarballs, add this config flag.
  # --enable-maintainer-mode

  # Build static C++ library, on Linux you can get a shared C++ library
  # but not so on Windows. We still ask for one anyway. The .pc files are
  # incorrect on all systems. They never refer to nlopt_cxx.
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX}          \
    --build=${MINGW_CHOST}            \
    --host=${MINGW_CHOST}             \
    --target=${MINGW_CHOST}           \
    --enable-static                   \
    --enable-shared                   \
    --with-cxx                        \
    --without-python

  # build system is a bit racy around the swig scm and scm.in file.
  make -j1
  cp "${srcdir}"/build-${CARCH}/.libs/libnlopt_cxx.a "${srcdir}"/build-${CARCH}/libnlopt_cxx.a
  
  # without this call, the resulting libraries do not contain the functions
  # pertaining to the Fortran API
  make distclean

  # Build static and shared C libraries.
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX}          \
    --build=${MINGW_CHOST}            \
    --host=${MINGW_CHOST}             \
    --target=${MINGW_CHOST}           \
    --enable-static                   \
    --enable-shared                   \
    --without-python

  # build system is a bit racy around the swig scm and scm.in file.
  make -j1
}

check() {
  cd "${srcdir}"/build-${CARCH}
  make check
}

package() {
  cd "${srcdir}"/build-${CARCH}
  make install DESTDIR="${pkgdir}"
  mv "${srcdir}"/build-${CARCH}/libnlopt_cxx.a "${pkgdir}"${MINGW_PREFIX}/lib
}
